package org.zyj.io.pipeline.duplex2;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.nio.charset.StandardCharsets;

public class DuplexHandlerInitializer extends ChannelInitializer<NioSocketChannel> {

    @Override
    protected void initChannel(NioSocketChannel ch) throws Exception {
        ch.pipeline() //配置pipeline
                .addLast(new DuplexHandlerInitializer.InboundHandler1())
                .addLast(new DuplexHandlerInitializer.InboundHandler2())
                .addLast(new DuplexHandlerInitializer.DuplexHandler1())
                .addLast(new DuplexHandlerInitializer.DuplexHandler2())
                .addLast(new DuplexHandlerInitializer.InboundHandler3())//这里write
        ;
    }

    static class InboundHandler1 extends ChannelInboundHandlerAdapter {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            ByteBuf byteBuf = (ByteBuf) msg;
            System.out.println("InboundHandler1; receive = " + byteBuf.toString(StandardCharsets.UTF_8));
            ctx.fireChannelRead(1);
        }
    }

    static class InboundHandler2 extends ChannelInboundHandlerAdapter {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            System.out.println("InboundHandler2; receive = " + msg.toString());
            ctx.fireChannelRead(2);
        }
    }


    static class DuplexHandler1 extends ChannelDuplexHandler {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            System.out.println("DuplexHandler1 channelRead; receive = " + msg.toString());
            ctx.fireChannelRead(1001);
        }

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            System.out.println("DuplexHandler1 write; msg = " + msg.toString());
            ctx.write(Unpooled.copiedBuffer("DuplexHandler1", StandardCharsets.UTF_8));
        }
    }

    static class DuplexHandler2 extends ChannelDuplexHandler {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            System.out.println("DuplexHandler2 channelRead; receive = " + msg.toString());
            ctx.fireChannelRead(1002);
        }

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            System.out.println("DuplexHandler2 write; msg = " + msg.toString());
            ctx.write(Unpooled.copiedBuffer("DuplexHandler2", StandardCharsets.UTF_8));
        }
    }


    static class InboundHandler3 extends ChannelInboundHandlerAdapter {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            System.out.println("InboundHandler3; receive = " + msg.toString());
            ctx.writeAndFlush(3);
        }
    }

}